using System;
using System.Linq;
using Microsoft.Xna.Framework;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GGJ11.Resources;
using LevelData;


namespace GGJ11
{
    public class LogoScreen : DrawableGameComponent
    {
        #region Fields

        public event Action OnLogoAppeared;

        private Rectangle texture_rectangle;
        private SpriteBatch sprite_batch;
        private Texture2D logo;

        private Animation animationLogo;

        private AnimationPlayer sprite;

        private string logo_path;
        private float alpha, time_to_wait;

        #endregion

        #region Constructors

        /// <summary>
        /// Constructors
        /// </summary>
        /// <param name="game">The game istance</param>
        /// <param name="OnLogoAppeared">Method to invoke once the logo is appeared</param>
        /// <param name="logo_path">Path of the texture, used to load the logo</param>
        public LogoScreen(MainGame game, Action OnLogoAppeared, string logo_path)
            : base(game)
        {
            this.logo_path = logo_path;
            this.OnLogoAppeared += new Action(OnLogoAppeared);
            time_to_wait = 1.8f;
        }

        #endregion

        public override void Initialize()
        {
            sprite_batch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

            base.Initialize();
        }

        protected override void LoadContent()
        {
            logo = Game.Content.Load<Texture2D>(logo_path);
            texture_rectangle = new Rectangle(Game.GraphicsDevice.Viewport.Width / 2 - 250,
                Game.GraphicsDevice.Viewport.Height / 2 - 125,
                500, 250);
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            alpha += dt / 2;

            if (alpha > time_to_wait)
            {
                OnLogoAppeared();
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            sprite_batch.Begin();

            sprite_batch.Draw(logo, texture_rectangle, Color.White);

            sprite_batch.End();

            base.Draw(gameTime);
        }
    }
}
